import { http } from "@/utils/http";

type Result = {
  total: number;
  elements?: Array<any>;
};

// 登录
export const loginTo = (data?: object) => {
  return http.request("post", "/api/auth/login", { data });
};

// 菜单分页列表
export const getMenusList = (data?: object) => {
  return http.request<Result>("get", "/api/system/menus", { data });
};

interface detailObject {
  id: number;
}
// 菜单详情
export const getMenusDetail = (data?: detailObject) => {
  return http.request<Result>("get", "/api/system/menus/" + data.id);
};

interface menusObject {
  id?: number;
}

// 新增菜单
export const addMenus = (data?: object) => {
  return http.request<menusObject>("post", "/api/system/menus", { data });
};

// 修改菜单
export const updateMenus = (data?: menusObject) => {
  return http.request<menusObject>("put", "/api/system/menus/" + data.id);
};

// 角色分页列表
export const getRolesList = (data?: object) => {
  return http.request<Result>("get", "/api/system/roles", { data });
};

// 角色详情
export const getRolesDetail = (data?: detailObject) => {
  return http.request<Result>("get", "/api/system/roles/" + data.id);
};

interface addObject {
  id?: number;
}

// 新增角色
export const addRoles = (data?: object) => {
  return http.request<addObject>("post", "/api/system/roles", { data });
};

// 修改角色
export const updateRoles = (data?: addObject) => {
  return http.request<addObject>("put", "/api/system/roles/" + data.id);
};

interface roleObject {
  id?: number;
  resource?: object;
  method?: number;
  resourceMappings?: any;
  menuIds?: any;
}

// 角色拥有资源列表
export const getRolesResources = (data?: addObject) => {
  return http.request<roleObject>(
    "get",
    "/api/system/roles/" + data.id + "/resources"
  );
};

// 修改角色关联资源
export const updateRolesResources = (data?: roleObject) => {
  return http.request<roleObject>(
    "put",
    "/api/system/roles/" + data.id + "/resources",
    data.resourceMappings
  );
};

// 角色关联菜单列表
export const getRolesMenus = (data?: addObject) => {
  return http.request<roleObject>(
    "get",
    "/api/system/roles/" + data.id + "/menus"
  );
};

// 修改角色关联菜单
export const updateRolesMenus = (data?: roleObject) => {
  return http.request<roleObject>(
    "put",
    "/api/system/roles/" + data.id + "/menus",
    data.menuIds
  );
};

interface pagingObject {
  page?: number;
  size?: number;
}

// 资源分页列表
export const getResourcesList = (data?: pagingObject) => {
  return http.request<Result>(
    "get",
    "/api/system/resources?page=" + data.page + "&size=" + data.size
  );
};

// 资源详情
export const getResourcesDetail = (data?: detailObject) => {
  return http.request<Result>("get", "/api/system/resources/" + data.id);
};

// 新增资源
export const addResources = (data?: object) => {
  return http.request<addObject>("post", "/api/system/resources", { data });
};

// 修改资源
export const updateResources = (data?: addObject) => {
  return http.request<addObject>("put", "/api/system/resources/" + data.id);
};

// 账户分页列表
export const getAccountsList = data => {
  return http.request<Result>(
    "get",
    "/api/system/accounts?page=" + data.page + "&size=" + data.size
  );
};

// 账户详情
export const getAccountsDetail = (data?: detailObject) => {
  return http.request<Result>("get", "/api/system/accounts/" + data.id);
};

// 新增账户
export const addAccounts = (data?: object) => {
  return http.request<addObject>("post", "/api/system/accounts", { data });
};

// 修改账户
export const updateAccounts = (data?: object) => {
  return http.request<addObject>("put", "/api/system/accounts", data);
};

interface roleObject {
  aid?: number;
  roleIds?: any;
}

// 查询账户拥有的角色列表
export const getAccountsRoleIds = (data?: roleObject) => {
  return http.request<Result>(
    "get",
    "/api/system/accounts/" + data.aid + "/roles"
  );
};

// 修改 账户 角色授权
export const updateAccountsRole = (data?: roleObject) => {
  return http.request<addObject>(
    "put",
    "/api/system/accounts/" + data.aid + "/roles",
    data.roleIds
  );
};

// 部门分页列表
export const getDeptsList = (data?: object) => {
  return http.request<Result>("get", "/api/system/departments", { data });
};

// 部门详情
export const getDeptsDetail = (data?: detailObject) => {
  return http.request<Result>("get", "/api/system/departments/" + data.id);
};

// 新增部门
export const addDept = (data?: object) => {
  return http.request<addObject>("post", "/api/system/departments", { data });
};

// 修改部门
export const updateDept = (data?: addObject) => {
  return http.request<addObject>("put", "/api/system/departments/" + data.id);
};
